package com.issac.mmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.issac.mmall.entity.Product;
import com.issac.mmall.entity.ProductCategory;
import com.issac.mmall.mapper.ProductCategoryMapper;
import com.issac.mmall.mapper.ProductMapper;
import com.issac.mmall.service.ProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.issac.mmall.vo.ProductCategoryVO;
import com.issac.mmall.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Issac
 * @since 2020-12-10
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<ProductCategoryVO> getAllProductCategoryVo() {
        //一级菜单
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type",1);
        List<ProductCategory> levelOne = productCategoryMapper.selectList(wrapper);
        //转VO
        List<ProductCategoryVO> levelOneVO = levelOne.stream().map(e -> new ProductCategoryVO(e.getId(),e.getName())).collect(Collectors.toList());
        //图片赋予
        //产品信息赋予
        for (ProductCategoryVO LevelOneProductCategoryVO : levelOneVO) {
            for (int i = 0; i < levelOne.size(); i++) {
                levelOneVO.get(i).setBannerImg("/images/banner"+i+".png");
                levelOneVO.get(i).setTopImg("/images/top"+i+".png");
                wrapper = new QueryWrapper();
                wrapper.eq("categorylevelone_id",levelOneVO.get(i).getId());
                List<Product> product = productMapper.selectList(wrapper);
                List<ProductVO> productVOList = product.stream()
                        .map( e-> new ProductVO(e.getId(),e.getName(),e.getPrice(),e.getFileName()))
                        .collect(Collectors.toList());
                levelOneVO.get(i).setProductVOList(productVOList);
            }
            wrapper = new QueryWrapper();
            wrapper.eq("type",2);
            wrapper.eq("parent_id",LevelOneProductCategoryVO.getId());
            List<ProductCategory> levelTwo = productCategoryMapper.selectList(wrapper);
            List<ProductCategoryVO> levelTwoVO = levelTwo.stream().map(e -> new ProductCategoryVO(e.getId(),e.getName())).collect(Collectors.toList());
            LevelOneProductCategoryVO.setChildren(levelTwoVO);
            for (ProductCategoryVO LevelTwoProductCategoryVO : levelTwoVO) {
                wrapper = new QueryWrapper();
                wrapper.eq("type",3);
                wrapper.eq("parent_id",LevelTwoProductCategoryVO.getId());
                List<ProductCategory> levelThree = productCategoryMapper.selectList(wrapper);
                List<ProductCategoryVO> levelThreeVO = levelThree.stream().map(e -> new ProductCategoryVO(e.getId(),e.getName())).collect(Collectors.toList());
                LevelTwoProductCategoryVO.setChildren(levelThreeVO);
            }
        }
        return levelOneVO;
    }
}
